<template>
	<view class="safe-bottom">
		<view class="">
			<supplier-order-address :address="address"></supplier-order-address>
		</view>
		<!-- 商品详情 -->
		<template v-if="shopList.length">
			<view class="" style="padding-bottom: 100rpx">
				<view class=" " v-for="(item, index) in shopList" :key="index">
					<supplier-order-item :index="index" :item="item" :is_special="is_special"
						@coupupSure="coupupSure"></supplier-order-item>
				</view>
				<view class="u-m-t-10 u-p-x-30 bg-white" v-if="is_special != 1">
				  <u-cell-item
				    :title="`该订单需要${need_credit_discount}记账点才能支付`"
				    :arrow="false"
				    hoverClass="none"
				  >
				  </u-cell-item>
				</view>
				<view class=" u-p-x-30 bg-white" v-if="is_special != 1">
				  <u-cell-item
				    :title="`您账户目前拥有${userInfo.credit4}记账点`"
				    :arrow="false"
				    hoverClass="none"
				  >
				  </u-cell-item>
				</view>
				<!-- <view class="u-m-b-10 u-p-x-30 bg-white" v-if="is_special != 1">
					<u-cell-item :title="`使用${detail.credit2_number}记账点抵扣${detail.credit_money}`" :arrow="false"
						hoverClass="none">
						<u-checkbox v-if="!!goods5_credit4_dikou && !(goods5_credit4_dikou > 0)" v-model="is_creditdiscount" style="margin-right: 0" shape="circle"
							@change="checkboxGroupClick"></u-checkbox>
					</u-cell-item>
				</view> -->
				<view class="u-m-y-10 u-p-x-30 bg-white">
					<u-radio-group  v-model="distribution_type" active-color="red" @change="radioGroupChange">
						<u-radio name="1">配送</u-radio>
						<u-radio name="2">自提</u-radio>
					</u-radio-group>
				</view>
			</view>
		</template>

		<view class="position-fixed left-0 right-0 bottom-0" style="z-index: 2">
			<view class="bg-white safe-bottom">
				<view class="flex align-center justify-between u-p-x-30 u-border-top" style="height: 100rpx">
					<view class="flex align-center">
						<text class="u-font-30 u-m-r-20">实付金额</text>
						<text class="u-font-30 text-danger">¥{{ totalPrice || "0.00" }}</text>
					</view>
					<view class="">
						<dy-button width="200rpx" backgroundColor="var(--mainColor)" size="medium"
							@click="sureOrder">立即支付</dy-button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		GoodsCenterApi,MineCenterApi
	} from "@/api/serves.js";
	import { Calc } from "@/common/utils/utils.js"
	import { mapGetters } from 'vuex'
	export default {
		data() {
			return {
				type: "common", // common商品下单  cart 购物车下单
				detail: {},
				shopList: [], // 店铺列表
				order_pay_price: "", // 实付金额
				address: null, // 收货地址
				is_special: 0,
				formData: [], // [{merch_id: '', goods_id: '', goods_sku_id: ''}]
				is_creditdiscount: true, // 是否使用记账点抵扣
				notes: [], // 备注
				couponObj: {}, // 优惠券Id和店铺id 键值对
				orderInfoList: [], // 优惠劵 优惠劵id 以及备注
				goodsType: 1,
				distribution_type:1, // 配送方式，1=配送，2=自提
				merch_express_price:'', //配送费
				goods5_credit4_dikou:'', // 商圈商品抵扣比例
				  need_credit_discount:'', // 下单需要记账点
			};
		},
		onLoad(options) {
			this.$store.dispatch('user/getUserInfo')
			uni.$on("addressClick", () => {
				this._orderAffirm();
			});
			console.log("options", options);
			let orderInfo = JSON.parse(decodeURIComponent(options.orderInfo));
			console.log('orderInfo',orderInfo);
			// console.log('orderInfo', orderInfo);
			this.type = options.type || "common";
			this.goodsType = options.goodsType || 1;
			this.formData = orderInfo;
		},
		onUnload() {
			uni.$off("addressClick");
		},
		onShow() {
			this._orderAffirm();
		},
		computed:{
			...mapGetters('user',['userInfo']),  
			// 计算总价
			totalPrice(){
				if(this.shopList[0]){
					let {merch_total_price,merch_express_price} = this.shopList[0]
					console.log('计算总价',Calc.Add(merch_total_price,merch_express_price));
					return Calc.Add(merch_total_price,merch_express_price)
					// return Number(merch_total_price) + Number(merch_express_price)
				}
				
			}
		},
		methods: {
			//切换配送方式
			radioGroupChange(e){
				this.distribution_type = e
				console.log('配送方式',e);
				console.log(9000, this.shopList);
				this.shopList[0].merch_express_price = e == 1 ? this.merch_express_price : 0
				this.shopList[0].merch_pay_price = this.totalPrice
			},
			// 商品下单详情
			_orderAffirm() {
				let params = this.formartParams();
				console.log(999,this.type, params);
				uni.showLoading();
				this.getConfig()
				if (this.type == "common") return this._orderCcmmonInfo(params);
				if (this.type == "cart") return this._orderCartInfo(params);
			},
			// 获取配置
			getConfig() {
				MineCenterApi.getConfig().then(res => {
					console.log('获取配置', res);
					if (res.code == 1) {
						this.goods5_credit4_dikou = res.data.data.goods5_credit4_dikou
						console.log('商圈商品抵扣比例',this.goods5_credit4_dikou);
					} else {
						console.log(res);
					}
				})
			},
			_orderCcmmonInfo(params) {
				console.log("params123",params);
				GoodsCenterApi.cartBuyNow({
					...params,
					goods_type: this.goodsType,
				}).then((res) => {
					uni.hideLoading();
					console.log("购买详情", res);
					if (res.code == 1) {
						this.formartRes(res);
					} else {
						this.$u.toast(res.msg);
					}
				});
			},
			_orderCartInfo(params) {
				GoodsCenterApi.getlists({
					...params,
					goods_type: this.goodsType
				}).then((res) => {
					uni.hideLoading();
					console.log("购买详情", res);
					if (res.code == 1) {
						this.formartRes(res);
					} else {
						this.$u.toast(res.msg);
					}
				});
			},
			formartRes(res) {
				console.log('formartRes', res);
				this.detail = res.data;
				this.address = res.data.address;
				this.need_credit_discount = res.data.need_credit_discount
				if (this.address) {
					this.address.Area = Object.values(this.address.Area);
				}
				if (Array.isArray(res.data.merch_goods_list)) {
					this.shopList = res.data.merch_goods_list;
					if (!this.orderInfoList.length)
						this.orderInfoList = new Array(this.shopList.length).fill("");
				} else {
					this.shopList = Object.values(res.data.merch_goods_list);
					if (!this.orderInfoList.length)
						this.orderInfoList = new Array(this.shopList.length).fill("");
				}
				this.merch_express_price = this.shopList[0].merch_express_price
				console.log(8998, this.shopList);
				this.order_pay_price = res.data.order_pay_price;
			},
			// 下单去支付
			sureOrder() {
				if (!this.address) return this.$Toast("请选择收货地址");
				let payParams = this.formartParams();
				payParams.note = this.orderInfoList
					.map((v) => {
						return v.note || "";
					})
					.join(",");
				let fun = this.type == "common" ? "cart_pay" : "cart_pay";
				console.log("payParams", payParams);
				this.$ShowLoading();
				GoodsCenterApi[fun]({
					...payParams,
					goods_type: this.goodsType,
					type: 1
				}).then((res) => {
					console.log("商品下单成功，待支付", res);
					this.$HideLoading();
					if (res.code == 1) {
						let params = {
							order_no: res.data.order_no,
							from: this.type,
						};
						uni.redirectTo({
							url: "/pages/goodsDetail/goodsOrderPay/goodsOrderPay?orderInfo=" +
								JSON.stringify(params),
						});
					} else {
						this.$Toast(res.msg);
					}
				});
			},
			formartParams() {
				let goods_id_list = [];
				let goods_sku_id_list = [];
				console.log(this.formData);
				this.formData.forEach((item) => {
					goods_id_list.push(item.goods_id);
					goods_sku_id_list.push(item.goods_sku_id);
				});
				console.log('goods_sku_id_list', goods_sku_id_list);
				let params = {
					goods_id: goods_id_list.join(","),
					goods_sku_id: goods_sku_id_list.join(","),
					distribution_type:this.distribution_type
				};
				if (this.type == "common") {
					params.merch_id = this.formData[0].merch_id
					params.type = 1
					params.coupon_id =
						(this.orderInfoList.length && this.orderInfoList[0].id) || "";
				}
				if (this.type == "cart") {
					params.type = 1;
					let merch_ids = [];
					let coupon_ids = [];
					for (let v of this.orderInfoList) {
						if (v && v.id) {
							merch_ids.push(v.merch_id);
							coupon_ids.push(v.id);
						}
					}
					params.merch_id = merch_ids.join(",");
					params.coupon_id = coupon_ids.join(",");
				}
				params.is_creditdiscount = this.is_creditdiscount ? 1 : 0;
				return params;
			},
			// 是否使用积分
			checkboxGroupClick(e) {
				console.log("是否使用积分", e);
				this.is_creditdiscount = e.value ? 1 : 0;
				console.log(11, this.orderInfoList);
				this._orderAffirm();
			},
			// 优惠劵选择
			coupupSure(e) {
				this.orderInfoList.splice(e.index, 1, e);
				// console.log(111, this.orderInfoList);
				this._orderAffirm();
			},
		},
	};
</script>

<style lang="scss" scoped>
	::v-deep .u-cell {
		padding: 15rpx 0 !important;
	}

	::v-deep .u-cell_title {
		font-weight: bold !important;
	}

	::v-deep .u-field {
		padding-left: 0 !important;
		padding-right: 0 !important;
	}

	::v-deep .u-checkbox__label {
		margin-right: 0;
	}
	::v-deep .u-radio-group{
		width: 100%;
		height: 78rpx;
		display: flex;
		align-items: center;
		background-color: #fff;
	}
</style>